﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Media.Animation;

namespace BombPart
{
    public delegate void BombedDelegator(Bomb bomb);

    public enum MovingMode
    {
        None,
        Left,
        Right,
        Forward
    }

    /// <summary>
    /// Bomb.xaml の相互作用ロジック
    /// </summary>
    public partial class Bomb : UserControl
    {
        public Bomb()
        {
            InitializeComponent();

            BombWidth = 134;
            BombHeight = 134;
        }

        public Storyboard MyStoryBoard { get; set; }
        public double BombWidth { get; private set; }
        public double BombHeight { get; private set; }

        public MovingMode MovingMode { get; set; }
        /// <summary>
        /// MovingModeがLeft、Rightの場合はボムの移動距離
        /// Forwardの場合は、拡大率
        /// </summary>
        public double MovingFactor { get; set; }
        public double MovingMilliseconds { get; set; }

        public void Fire()
        {
            TranslateTransform moving = null;
            ScaleTransform multing = null;
            if (MovingMode == MovingMode.Left || MovingMode == MovingMode.Right)
            {
                moving = new TranslateTransform(0, 0);
                this.RenderTransform = moving;
            }
            else if (MovingMode == BombPart.MovingMode.Forward)
            {
                multing = new ScaleTransform();
                multing.CenterX = 67;
                multing.CenterY = 67;
                this.RenderTransform = multing;
            }

            MyStoryBoard = this.Resources["myHorizontalOffset"] as Storyboard;
            MyStoryBoard.Completed += new EventHandler(MyStoryBoard_Completed);
            MyStoryBoard.Begin();

            if (moving != null)
            {
                moving.BeginAnimation(TranslateTransform.XProperty,
                    new DoubleAnimation(MovingFactor,
                        new Duration(TimeSpan.FromMilliseconds(MovingMilliseconds))));
            }
            if (multing != null)
            {
                multing.BeginAnimation(ScaleTransform.ScaleXProperty,
                    new DoubleAnimation(MovingFactor,
                        new Duration(TimeSpan.FromMilliseconds(MovingMilliseconds))));
                multing.BeginAnimation(ScaleTransform.ScaleYProperty,
                    new DoubleAnimation(MovingFactor,
                        new Duration(TimeSpan.FromMilliseconds(MovingMilliseconds))));
            }
        }

        void MyStoryBoard_Completed(object sender, EventArgs e)
        {
            if (Completed != null)
            {
                Completed(this);
            }
        }

        public event BombedDelegator Completed;
    }
}
